package com.config;


import com.alibaba.fastjson.JSONObject;
import com.config.log.LogFactory;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @USER: Ruiwen
 */
public class AspectUtil {
    /**
     * 解析请求参数
     */
    public static String getParams(JoinPoint joinPoint) {
        String params = "";
        if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {

            // 参数名称
            String[] names = ((MethodSignature) joinPoint.getSignature()).getParameterNames();

            for (int i = 0; i < joinPoint.getArgs().length; i++) {
                Object arg = joinPoint.getArgs()[i];
                if ((arg instanceof HttpServletResponse) || (arg instanceof HttpServletRequest)
                        || (arg instanceof MultipartFile) || (arg instanceof MultipartFile[])) {
                    continue;
                }
                try {
                    if (params.length() == 0) {
                        params += ("" + names[i] + "=" + JSONObject.toJSONString(joinPoint.getArgs()[i]));
                    } else {
                        params += ("," + names[i] + "=" + JSONObject.toJSONString(joinPoint.getArgs()[i]));
                    }
                } catch (Exception e1) {
                    LogFactory.printErrLog(e1.getMessage());
                }
            }
        }
        return params;
    }

}
